﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace _1stVATCWed
{
    public partial class Shopping : System.Web.UI.Page
    {
        private string IDsanpham ;
        protected void Page_Load(object sender, EventArgs e)
        {
            IDsanpham = Request.QueryString["id"];
            if(!IsPostBack)
            {
                if (IDsanpham != null)
                {
                    string connectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
                    SqlConnection conn = new SqlConnection(connectionString);
                    string sql = "select IDsanpham,tensanpham,giaban from SANPHAM where IDsanpham =@IDsanpham";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    conn.Open();
                    cmd.Parameters.AddWithValue("@IDsanpham", IDsanpham);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);
                    String tensanpham = dt.Rows[0][1].ToString();
                    int dongia = int.Parse(dt.Rows[0][2].ToString());
                    int slxuat = 1;
                    ThemVaoGioHang(IDsanpham, tensanpham, dongia, slxuat);
                    dt = (DataTable) Session["GioHang"];
                    Repeater1.DataSource = dt.DefaultView;
                    Repeater1.DataBind();                  
                }
                if (Session["GioHang"] != null)
                {
                    DataTable dt = new DataTable();
                    dt = (DataTable)Session["GioHang"];
                    System.Decimal tongThanhTien = 0;
                    foreach (DataRow r in dt.Rows)
                    {
                        r["Thanhtien"] = Convert.ToInt32(r["slxuat"]) * Convert.ToDecimal(r["giaban"]);
                        tongThanhTien += Convert.ToDecimal(r["Thanhtien"]);
                        LabelTongTien.Text = tongThanhTien.ToString();
                    }
                    Repeater1.DataSource = dt.DefaultView;
                    Repeater1.DataBind();
                }
            }
        }
        public void ThemVaoGioHang(string IDsanpham, string tensanpham, int dongiaxuat, int slxuat)
        {
            DataTable dt;
            if (Session["GioHang"] == null)
            {
                
                dt = new DataTable();
                dt.Columns.Add("IDsanpham");
                dt.Columns.Add("tensanpham");
                dt.Columns.Add("giaban");
                dt.Columns.Add("slxuat");
                dt.Columns.Add("Thanhtien");
            }
            else
            dt = (DataTable)Session["GioHang"];
            int dong = SPdacotronggiohang(IDsanpham, dt);
            if (dong != -1)
            {
                dt.Rows[dong]["slxuat"] = Convert.ToInt32(dt.Rows[dong]["slxuat"].ToString()) + slxuat;
            }
            else
            {
                DataRow dr = dt.NewRow();
                dr["IDsanpham"] = IDsanpham;
                dr["tensanpham"] = tensanpham;
                dr["slxuat"] = slxuat;
                dr["giaban"] = dongiaxuat;
                dr["Thanhtien"] = dongiaxuat * slxuat;
                dt.Rows.Add(dr);
            }
            Session["GioHang"] = dt;
        }
        public static int SPdacotronggiohang(string idsanpham, DataTable dt)
        {
            int dong = -1;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["IDsanpham"].ToString() == idsanpham)
                {
                    dong = i;
                    break;
                }
            }
            return dong;
        }
        protected void ImageButtonDatmua_Click(object sender, ImageClickEventArgs e)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "insert into PHIEUXUAT(IDphieuxuat,ngayxuat,hotenKH,diachi,CMND,email,sodt,IDgiaonhan,IDthanhtoan,nguoinhanhang,xuathoadon) VALUES(@IDphieuxuat,@ngayxuat,@hotenKH,@diachi,@CMND,@email,@sodt,@IDgiaonhan,@IDthanhtoan,@nguoinhanhang,@xuathoadon)";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@IDphieuxuat", 0);
            cmd.Parameters.AddWithValue("@ngayxuat", DateTime.Today);
            cmd.Parameters.AddWithValue("@hotenKH", TextBoxhotenKH.Text);
            cmd.Parameters.AddWithValue("@diachi", TextBoxdiachi.Text);
            cmd.Parameters.AddWithValue("@CMND", TextBoxCMND.Text);
            cmd.Parameters.AddWithValue("@email", TextBoxEmail.Text);
            cmd.Parameters.AddWithValue("@sodt", TextBoxsodt.Text);
            if(checkboxXuatHoadon.Checked)
            {
                cmd.Parameters.AddWithValue("@xuathoadon", "true");
            }
            else
            {
                cmd.Parameters.AddWithValue("@xuathoadon", "false");
            }
            if(CheckBoxNguoinhanhang.Checked)
            {
                cmd.Parameters.AddWithValue("@nguoinhanhang", "true");
            }
            else
            {
                cmd.Parameters.AddWithValue("@nguoinhanhang", "false");
            }
            if(RadioMienPhi.Checked)
            {
                cmd.Parameters.AddWithValue("@IDgiaonhan", "N1");
            }
            else
            {
                cmd.Parameters.AddWithValue("@IDgiaonhan", "N2");
            }
            foreach (ListItem item in RadioButtonList1.Items)
            {
                if(item.Value =="1"&& item.Selected)
                {
                    cmd.Parameters.AddWithValue("@IDthanhtoan", "T2");
                }
                else
                {
                    if(item.Value =="2"&& item.Selected)
                    {
                        cmd.Parameters.AddWithValue("@IDthanhtoan", "T1");
                    }
                    else
                    {
                        if(item.Value == "3"&& item.Selected)
                        {
                            cmd.Parameters.AddWithValue("@IDthanhtoan", "T3");
                        }   
                    }
                }
            }
           cmd.CommandText = "insert into PHIEUXUAT_CT (IDsanpham,slxuat,dongiaxuat) values(@IDsanpham,@slxuat,@dongiaxuat)";
            cmd.Parameters.Clear();
           DataTable dt = (DataTable) Session["GioHang"];
            foreach (DataRow dr in dt.Rows)
            {
                cmd.Parameters.AddWithValue("@IDsanpham", dr["IDsanpham"]);
                cmd.Parameters.AddWithValue("@slxuat", dr["slxuat"]);
                cmd.Parameters.AddWithValue("@dongiaxuat", dr["Thanhtien"]);     
            }
            cmd.ExecuteNonQuery();
            conn.Close();
            TextBoxhotenKH.Text = "";
            TextBoxCMND.Text = "";
            TextBoxdiachi.Text = "";
            TextBoxEmail.Text = "";
            Session.RemoveAll();
        }
    }
}